package indi.jp.project.core.controller;

import indi.jp.project.core.entity.Dictionary;
import indi.jp.project.core.entity.DictionaryGroup;
import indi.jp.project.core.entity.JsonResult;
import indi.jp.project.core.service.DictionaryService;
import indi.jp.project.core.util.SimpleEntityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys/dictionary")
public class DictionaryController {

    @Autowired
    private DictionaryService dictionaryService;

    @GetMapping("/queryGroupList")
    @ResponseBody
    public JsonResult queryGroupList(HttpServletRequest request) {

        // 这里用实体类是为了后期改懒加载加参数
        DictionaryGroup entity = new DictionaryGroup();
        try {
            List<DictionaryGroup> list = dictionaryService.queryGroupList(entity);
            return JsonResult.success(list);
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @PostMapping("/saveGroup")
    @ResponseBody
    public JsonResult saveGroup(@RequestBody DictionaryGroup entity, HttpServletRequest request){
        SimpleEntityUtil<DictionaryGroup> simpleEntityUtil = new SimpleEntityUtil<>();
        entity = simpleEntityUtil.setBaseEntity(entity, request);
        try {
            int effectRow = dictionaryService.saveGroup(entity);
            if(effectRow > 0){
                return JsonResult.success("");
            } else {
                return JsonResult.failure("保存失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @PostMapping("/deleteGroupById")
    @ResponseBody
    public JsonResult deleteGroupById(HttpServletRequest request){
        String id = request.getParameter("id");
        try {
            int effectRow = dictionaryService.deleteGroupById(id);
            if(effectRow > 0){
                return JsonResult.success("");
            } else {
                return JsonResult.failure("未能成功删除，请刷新查看数据是否存在");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @GetMapping("/queryGrid")
    @ResponseBody
    public JsonResult queryGrid(@ModelAttribute Dictionary entity, HttpServletRequest request) {
        SimpleEntityUtil<Dictionary> simpleEntityUtil = new SimpleEntityUtil<>();
        entity = simpleEntityUtil.setBaseEntity(entity, request);
        try {
            Map<String, Object> map = dictionaryService.queryGrid(entity);
            return JsonResult.success(map);
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @PostMapping("/save")
    @ResponseBody
    public JsonResult save(@RequestBody Dictionary entity, HttpServletRequest request){
        SimpleEntityUtil<Dictionary> simpleEntityUtil = new SimpleEntityUtil<>();
        entity = simpleEntityUtil.setBaseEntity(entity, request);
        try {
            int effectRow = dictionaryService.save(entity);
            if(effectRow > 0){
                return JsonResult.success("");
            } else {
                return JsonResult.failure("保存失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @PostMapping("/deleteById")
    @ResponseBody
    public JsonResult deleteById(HttpServletRequest request){
        String id = request.getParameter("id");
        try {
            int effectRow = dictionaryService.deleteById(id);
            if(effectRow > 0){
                return JsonResult.success("");
            } else {
                return JsonResult.failure("未能成功删除，请刷新查看数据是否存在");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @GetMapping("/queryByGroups")
    @ResponseBody
    public  JsonResult queryByGroups(HttpServletRequest request){
        String groupCodes = request.getParameter("groupCodes");
        if("".equals(groupCodes)){
            return JsonResult.failure("缺少参数");
        }
        Map<String, Object> map = new HashMap<>();
        String[] codes = groupCodes.split(",");
        map.put("groupCodes",codes);
        try {
            return dictionaryService.queryByGroups(map);
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }
}
